/*
使用方法
引用
#include "CommonLog.mqh";
声明
private:
 CommonLog *logger;
...
初始化
  logger = new CommonLog(i_printLevel);
输出信息
  logger.debug(StringFormat("当前Ask价: %G,止损值: %f", Ask, stoploss));
  logger.info(msg + "成功!");
*/
//+------------------------------------------------------------------+
#ifndef  _COMMONLOG_H
  #define _COMMONLOG_H

  #property copyright "Copyright 2022, MetaQuotes Software Corp."
  #property link      "https://www.mql5.com"
  #property version   "1.00"
  
  // #include "Common.mqh"


  class CommonLog
  {
  private:

    // 打印级别, 数值越小输出越少
    short printLevel_;
    // ENUM_TIMEFRAMES i_shortTF;
    int int_start_time_;
    // 取整函数
    static datetime roundTime(datetime src_time, ENUM_TIMEFRAMES timeframe);

  public:
    enum enumLogLevel
    {
      FATAL = 0,
      ERROR,
      WARN,
      INFO,
      DEBUG,
      TRACE
    };

    // 输出信息

    void logPrint(string format_str,  enumLogLevel logLevel);
    // error 级以下不输出
    void error(string format_str);  
    // debug 级以下不输出
    void debug(string format_str);  
    // warn 级以下不输出
    void warn(string format_str); 
    // info 级以下不输出
    void info(string format_str); 
    CommonLog(short printLevel);
    CommonLog(short printLevel, int int_start_time);
    // CommonLog(short printLevel, ENUM_TIMEFRAMES i_shortTF, int int_start_time);
    ~CommonLog();
  };
  /**
   * 对指定时间按指定周期长度取整
   * src_time 待取整的时间
   * timeframe 周期
   */
  datetime CommonLog::roundTime(datetime src_time, ENUM_TIMEFRAMES timeframe)
  {
    datetime remain_time = src_time % (timeframe * 60);
    return (src_time - remain_time);
  }

  CommonLog::CommonLog(short printLevel, int int_start_time)
  {
    printLevel_ = printLevel;
    int_start_time_ = int_start_time;
  }
  // CommonLog::CommonLog(short printLevel, ENUM_TIMEFRAMES i_shortTF, int int_start_time)
  // {
  //   printLevel_ = printLevel;
  //   i_shortTF = i_shortTF;
  //   int_start_time_ = int_start_time;
  // }
  //+------------------------------------------------------------------+
  CommonLog::CommonLog(short printLevel)
  {
    printLevel_ = printLevel;
  }
  void CommonLog::logPrint(string format_str,  enumLogLevel logLevel)
  {
    datetime curr_time = TimeCurrent();
    if (int_start_time_ == 0 || TimeCurrent() >= int_start_time_)
    {
      if (printLevel_ >= logLevel)
        Print(format_str);
    }
  }
  void CommonLog::error(string format_str)
  {
      logPrint(format_str, enumLogLevel::ERROR);
  }
  void CommonLog::debug(string format_str)
  {
    logPrint(format_str, enumLogLevel::DEBUG);
  }

  /**
   * 程序出现了不希望出现的情况
   */
  void CommonLog::warn(string format_str)
  {
    logPrint(format_str, enumLogLevel::WARN);
  }

  void CommonLog::info(string format_str)
  {
      logPrint(format_str, enumLogLevel::INFO);
  }
  //+------------------------------------------------------------------+
  CommonLog::~CommonLog()
  {
  }
  //+------------------------------------------------------------------+
#endif